﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Lab1
{
    public class AnswerService
    {
        private DatabaseEntities db = new DatabaseEntities();
        public IEnumerable<AnswerVariant> getAll()
        {
            List<AnswerVariant> list = (from t in db.AnswerVariant
                                   select t).ToList();
            return list;
        }
        public void update(AnswerVariant model)
        {

            using (DatabaseEntities context = new DatabaseEntities())
            {
                var original = (from t in context.AnswerVariant
                                where t.Id.Equals(model.Id)
                                select t).SingleOrDefault();
                context.AnswerVariant.ApplyCurrentValues(model);
                context.SaveChanges();
            }

        }
        public void save(AnswerVariant model)
        {
            db.AddToAnswerVariant(model);
            db.SaveChanges();
        }
        public void delete(AnswerVariant model)
        {
            using (DatabaseEntities context = new DatabaseEntities())
            {
                var original = (from t in context.AnswerVariant
                                where t.Id.Equals(model.Id)
                                select t).SingleOrDefault();
                context.DeleteObject(original);
                context.SaveChanges();
            }
        }
        public AnswerVariant getById(Guid id)
        {
            var model= (from t in db.AnswerVariant
                    where t.Id.Equals(id)
                    select t).Single<AnswerVariant>();
           // db.Detach(model);
            return model;
        }
        public IEnumerable<AnswerVariant> getByQuestion(Guid questionId)
        {
            return (from t in db.AnswerVariant
                    where t.QuestionParentId.Equals(questionId)
                    select t).ToList<AnswerVariant>();
        }
    }
}
